package com.beijing.security;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.web.filter.PathMatchingFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 检查form是否正在被处理，从服务器端避免重复提交<br>
 * 限制：依赖Session、表单<input name="__token" type="hidden" value="genarated token"/><br>
 * 效果：第一次提交后，服务器正常处理；再次提交，跳转到Busy页面，提示“处理中”；
 * 		在Busy页面点击“刷新”，等到服务器处理完第一次的请求后，跳转到正常的处理页面。
 * @author zhanglizhi
 *
 */
public class FormIsProcessingFilter extends PathMatchingFilter{
	
	private static final Logger logger = LoggerFactory.getLogger(FormIsProcessingFilter.class);
	
    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) {
		HttpServletRequest reqs = (HttpServletRequest)request;
		if (null != reqs)
			logger.info(reqs.getRequestURL().toString());
		
		// 检查是request否包含__token，不包含则报错
		
		// 如果session中包含同样的token
			// 获取处理状态，如过正在处理：跳转到Busy
			// 如果处理完成，获取应该跳转的实际页面，跳转
		// 如果不包含，报错
        return true;
    }
    
    @Override
    protected void postHandle(ServletRequest request, ServletResponse response) throws Exception {
    	HttpServletResponse resp = (HttpServletResponse)response;
    	String str = resp.toString();
    }
}
